package httpx

import (
  "net"
  "time"
)

type tcpKeepAliveListener struct {
  *net.TCPListener
}

func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
  tc, err := ln.AcceptTCP()
  if err != nil {
    return nil, err
  }
  tc.SetKeepAlive(true)
  tc.SetKeepAlivePeriod(3 * time.Minute)
  return tc, nil
}

func Listen(addr string) (net.Listener, error) {
  if addr == "" {
    addr = ":http"
  }
  ln, err := net.Listen("tcp4", addr)
  if err != nil {
    return nil, err
  }
  return tcpKeepAliveListener{ln.(*net.TCPListener)}, nil
}
